<?php
function formatBytes($size) {
    if($size <= 1024) return '1 KB';
    $units = array(' B',' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return floor($size).$units[$i];
}
$readDir = trim(str_replace('../', '', (isset($_GET['dir']) ? $_GET['dir'] : '')), '/');
$dirList = empty($readDir) ? [] : explode('/', $readDir);
//忽略文件 小写
$ingoreList = ['.', '..', 'index.php', '.ds_store'];
?>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>File Browser</title>
    <style>
        *{margin:0;padding:0;box-sizing:border-box}
        body{font-family:"pingfang sc","microsoft yahei",Arial,monospace}
        a{text-decoration:none;color:#b483ff}
        ul,ol,li{list-style:none}
        .container{max-width:980px;margin-right:auto;margin-left:auto}
        nav{background-color:#ecf0f1}
        .breadcrumb{padding:15px 0}
        .breadcrumb:after{content:' ';display:block;clear:both}
        .breadcrumb-item{float:left}
        .breadcrumb-item+.breadcrumb-item::before{display:inline-block;padding-right:5px;padding-left:5px;color:#6c757d;content:"/"}
        .error-notice{position:relative;padding:13px 10px;margin:10px 0;border:1px solid #ffeeba;border-radius:.25rem;color:#856404;background-color:#fff3cd}
        .table{width:100%;border-collapse:collapse;margin:0 -5px;color:#999}
        .table td,table th{text-align:left;vertical-align:middle;padding:10px 5px}
        .table-hover tbody tr:hover{background-color:#eee}
        .table-hover tbody a{display:block}
        .footer{text-align: center;color:#999;font-size: 14px;border-top:solid 1px #efefef;padding-top:10px;margin-top:30px;}
        @media (min-width:768px){.container{width:750px}}
        @media (min-width:992px){.container{width:970px}}
        @media (min-width:1200px){.container{width:1170px}}
        @media (min-width:1200px){.container{width:1170px}}
    </style>
</head>
<body>

<nav aria-label="breadcrumb">
    <div class="container">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="?dir=/">Home</a></li>
            <?php $temp_prefix = '';
            foreach ($dirList as $i => $dir): ?>
                <li class="breadcrumb-item">
                    <?php if ($i < count($dirList) - 1): ?>
                        <a href="?dir=<?php echo $temp_prefix . $dir;
                        $temp_prefix = $dir . '/'; ?>"><?php echo $dir; ?></a>
                    <?php else: ?>
                        <?php echo $dir; ?>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ol>
    </div>
</nav>
<div class="container">

    <?php if (!file_exists(__DIR__ . '/' . $readDir) || !is_dir(__DIR__ . '/' . $readDir)): ?>
    <div class="error-notice">
        <h3>读取的文件夹不存在</h3>
        <a href="javascript:history.back();">返回</a>
    </div>
    <?php else: ?>
        <table class="table table-hover">
            <thead>
            <tr>
                <th scope="col">Name</th>
                <th scope="col" width="100">Type</th>
                <th scope="col" width="150">Size</th>
                <th scope="col" width="200">Last Modify</th>
            </tr>
            </thead>
            <tbody>
            <?php
            $files = scandir(__DIR__ . '/' . $readDir);
            foreach ($files as $file) :
                if (in_array(strtolower($file), $ingoreList)) continue;

                $read_filename = __DIR__ . '/' . $readDir . '/' . $file;
                $is_file = is_file($read_filename);
                $access_file = ltrim($readDir . '/' . $file,'/');
                $file_time = date('Y-m-d H:i:s', filemtime($read_filename));
                $file_size = $is_file ? formatBytes(filesize($read_filename)) : '-';
                ?>
                <tr>
                    <th scope="row">
                        <a href="<?php
                        echo $is_file ? $access_file.'" target="_blank' : '?dir=' . $access_file;
                        ?>"><?php echo $file; ?></a>
                    </th>
                    <td><?php echo $is_file ? '文件' : '目录'; ?></td>
                    <td><?php echo $file_size; ?></td>
                    <td><?php echo $file_time; ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
<footer class="footer">
    <a href="//pro.xiaoyan.me/sfb/" target="_blank">Simple File Browser</a>
</footer>
</body>
</html>